package com.feeyo.groundservice.recordnfc.dlg

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Text
import androidx.compose.runtime.MutableLongState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.lifecycle.setViewTreeLifecycleOwner
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.savedstate.setViewTreeSavedStateRegistryOwner
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ScreenUtils
import com.feeyo.groundservice.base_util.SuccessCallback
import com.feeyo.groundservice.base_util.dealString
import com.feeyo.groundservice.base_util.formatTimeYMDHM
import com.feeyo.groundservice.base_util.toastCenter
import com.feeyo.groundservice.composewidget.util.dimenToDp
import com.feeyo.groundservice.composewidget.util.dimenToSp
import com.feeyo.groundservice.composewidget.util.toComposeColor
import com.feeyo.groundservice.composewidget.view.button.CustomButton
import com.feeyo.groundservice.composewidget.view.util.VerticalSpace
import com.feeyo.groundservice.recordnfc.R
import com.feeyo.groundservice.recordnfc.activity.NfcLogActivity
import com.feeyo.groundservice.recordnfc.activity.SearchActivity
import com.feeyo.groundservice.recordnfc.constant.RecorderType
import com.feeyo.groundservice.recordnfc.constant.statusBarColor
import com.feeyo.groundservice.recordnfc.databinding.ViewCommonPopBinding
import com.feeyo.groundservice.recordnfc.model.NfcBindItemBO
import com.feeyo.groundservice.recordnfc.viewmodel.RecorderCardViewModel
import com.lxj.xpopup.core.BottomPopupView

/**
 *作者：70893 on 2025-10-13
 *描述：
 */
internal class NfcBindDetailPopView(context: Context) : BottomPopupView(context) {

    init {
        id = android.R.id.content
        setViewTreeLifecycleOwner(context as AppCompatActivity)
        setViewTreeSavedStateRegistryOwner(context as AppCompatActivity)
    }

    private lateinit var binding: ViewCommonPopBinding
    private val data = mutableStateOf<NfcBindItemBO?>(null)
    private var refreshFlag: MutableLongState? = null
    override fun getImplLayoutId(): Int {
        return R.layout.view_common_pop
    }

    override fun onCreate() {
        super.onCreate()
        binding = ViewCommonPopBinding.bind(this.popupImplView)
        binding.popCp.apply {
            setContent {
                val mContext = LocalContext.current
                val viewModel: RecorderCardViewModel = viewModel(mContext as AppCompatActivity)
                Column(
                    modifier = Modifier.fillMaxWidth()
                        .verticalScroll(rememberScrollState())
                        .background(
                            color = R.color.white.toComposeColor(),
                            shape = RoundedCornerShape(R.dimen.d8.dimenToDp())
                        )
                        .padding(R.dimen.d15.dimenToDp())
                ) {
                    Text(
                        text = "卡详情",
                        fontSize = R.dimen.d15.dimenToSp(),
                        fontWeight = FontWeight.W500,
                        color = R.color.black_33.toComposeColor(),
                        modifier = Modifier.align(Alignment.CenterHorizontally)
                    )

                    VerticalSpace(R.dimen.d15.dimenToDp())

                    Text(
                        text = "卡号：${data.value?.nfc.dealString()}",
                        fontSize = R.dimen.d14.dimenToSp(),
                        fontWeight = FontWeight.W400,
                        color = R.color.black_33.toComposeColor(),
                    )

                    VerticalSpace(R.dimen.d10.dimenToDp())

                    val type = when (data.value?.nfc_type) {
                        RecorderType.PERSON.type -> RecorderType.PERSON.title
                        RecorderType.CAR.type -> RecorderType.CAR.title
                        RecorderType.TASK.type -> RecorderType.TASK.title
                        else -> ""
                    }
                    Text(
                        text = "类型：${type.dealString()}",
                        fontSize = R.dimen.d14.dimenToSp(),
                        fontWeight = FontWeight.W400,
                        color = R.color.black_33.toComposeColor(),
                    )

                    VerticalSpace(R.dimen.d10.dimenToDp())
                    //特有字段
                    when (data.value?.nfc_type) {
                        RecorderType.PERSON.type -> {
                            Text(
                                text = "姓名：${data.value?.personName.dealString()}",
                                fontSize = R.dimen.d14.dimenToSp(),
                                fontWeight = FontWeight.W400,
                                color = R.color.black_33.toComposeColor(),
                            )
                            VerticalSpace(R.dimen.d10.dimenToDp())
                        }

                        RecorderType.CAR.type -> {
                            Text(
                                text = "车牌号：${data.value?.carNum.dealString()}",
                                fontSize = R.dimen.d14.dimenToSp(),
                                fontWeight = FontWeight.W400,
                                color = R.color.black_33.toComposeColor(),
                            )
                            VerticalSpace(R.dimen.d10.dimenToDp())
                        }

                        RecorderType.TASK.type -> {
                            Text(
                                text = "任务：${data.value?.taskName.dealString()}",
                                fontSize = R.dimen.d14.dimenToSp(),
                                fontWeight = FontWeight.W400,
                                color = R.color.black_33.toComposeColor(),
                            )
                            VerticalSpace(R.dimen.d10.dimenToDp())
                            Text(
                                text = "位置：${data.value?.taskLinkName.dealString()}",
                                fontSize = R.dimen.d14.dimenToSp(),
                                fontWeight = FontWeight.W400,
                                color = R.color.black_33.toComposeColor(),
                            )
                            VerticalSpace(R.dimen.d10.dimenToDp())

                        }

                        else -> {}
                    }

                    Text(
                        text = "录入人：${data.value?.username.dealString()}",
                        fontSize = R.dimen.d14.dimenToSp(),
                        fontWeight = FontWeight.W400,
                        color = R.color.black_33.toComposeColor(),
                    )
                    VerticalSpace(R.dimen.d10.dimenToDp())
                    Text(
                        text = "录入时间：${
                            data.value?.updated_time?.formatTimeYMDHM().dealString()
                        }",
                        fontSize = R.dimen.d14.dimenToSp(),
                        fontWeight = FontWeight.W400,
                        color = R.color.black_33.toComposeColor(),
                    )

                    VerticalSpace(R.dimen.d15.dimenToDp())
                    CustomButton(
                        modifier = Modifier.fillMaxWidth().height(R.dimen.d36.dimenToDp()),
                        bgs = listOf(
                            statusBarColor.toComposeColor(),
                            statusBarColor.toComposeColor().copy(0.8f)
                        ),
                        cornerShape = RoundedCornerShape(R.dimen.d5.dimenToDp()),
                        onClick = {
                            data.value?.id?.let {
                                viewModel.unbindNfc(it, object : SuccessCallback {
                                    override fun success() {
                                        super.success()
                                        dismiss()
                                        toastCenter("解绑成功")
                                        refreshFlag?.longValue = System.currentTimeMillis()
                                        if (ActivityUtils.getTopActivity()?.javaClass?.simpleName == SearchActivity::class.java.simpleName) {
                                            //在搜索页面进行解绑，需要同步刷新管理页面,静默刷新
                                            NfcLogActivity.showLoading = false
                                            NfcLogActivity.currentRefreshFlag.longValue =
                                                System.currentTimeMillis()
                                        }
                                    }
                                })
                            }
                        }
                    ) {
                        Text(
                            text = "解绑",
                            fontSize = R.dimen.d14.dimenToSp(),
                            fontWeight = FontWeight.W400,
                            color = R.color.white.toComposeColor(),
                            modifier = Modifier.align(Alignment.Center)
                        )
                    }
                }
            }
        }

    }

    fun setData(data: NfcBindItemBO?) {
        this.data.value = data
    }

    fun setRefreshFlag(refreshFlag: MutableLongState) {
        this.refreshFlag = refreshFlag
    }

    override fun getMaxHeight(): Int {
        return (ScreenUtils.getScreenHeight() * 0.4).toInt()
    }
}